package com.lwf.smartseniorcare.controller;

import com.lwf.smartseniorcare.dto.HealthRecordDTO;
import com.lwf.smartseniorcare.service.HealthRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/health-records")
public class HealthRecordController {

    @Autowired
    HealthRecordService healthRecordService;

    // Get all health records - only accessible to ADMIN
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping
    public ResponseEntity<List<HealthRecordDTO>> getAllHealthRecords() {
        List<HealthRecordDTO> records = healthRecordService.getAllHealthRecords();
        return ResponseEntity.ok(records);
    }

    // Get health record by ID
    @GetMapping("/{id}")
    public ResponseEntity<HealthRecordDTO> getHealthRecordById(@PathVariable Long id) {
        HealthRecordDTO record = healthRecordService.getHealthRecordById(id);
        return ResponseEntity.ok(record);
    }

    // Create health record - only accessible to ADMIN, DOCTOR, and CARETAKER
    @PreAuthorize("hasRole('ADMIN') or hasRole('DOCTOR') or hasRole('CARETAKER')")
    @PostMapping
    public ResponseEntity<HealthRecordDTO> createHealthRecord(@RequestBody HealthRecordDTO healthRecordDTO) {
        HealthRecordDTO createdRecord = healthRecordService.createHealthRecord(healthRecordDTO);
        return ResponseEntity.ok(createdRecord);
    }

    // Update health record - only accessible to ADMIN, DOCTOR, and CARETAKER
    @PreAuthorize("hasRole('ADMIN') or hasRole('DOCTOR') or hasRole('CARETAKER')")
    @PutMapping("/{id}")
    public ResponseEntity<HealthRecordDTO> updateHealthRecord(@PathVariable Long id, @RequestBody HealthRecordDTO healthRecordDTO) {
        HealthRecordDTO updatedRecord = healthRecordService.updateHealthRecord(id, healthRecordDTO);
        return ResponseEntity.ok(updatedRecord);
    }

    // Delete health record - only accessible to ADMIN
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteHealthRecord(@PathVariable Long id) {
        healthRecordService.deleteHealthRecord(id);
        return ResponseEntity.ok("Health record deleted successfully");
    }

    // Get health records by elderly
    @GetMapping("/elderly/{elderlyId}")
    public ResponseEntity<List<HealthRecordDTO>> getHealthRecordsByElderly(@PathVariable Long elderlyId) {
        List<HealthRecordDTO> records = healthRecordService.getHealthRecordsByElderly(elderlyId);
        return ResponseEntity.ok(records);
    }

    // Get health records by elderly and date range
    @GetMapping("/elderly/{elderlyId}/date-range")
    public ResponseEntity<List<HealthRecordDTO>> getHealthRecordsByElderlyAndDateRange(
            @PathVariable Long elderlyId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endDate) {
        List<HealthRecordDTO> records = healthRecordService.getHealthRecordsByElderlyAndDateRange(elderlyId, startDate, endDate);
        return ResponseEntity.ok(records);
    }

    // Get recent health records by elderly
    @GetMapping("/elderly/{elderlyId}/recent")
    public ResponseEntity<List<HealthRecordDTO>> getRecentHealthRecordsByElderly(@PathVariable Long elderlyId) {
        List<HealthRecordDTO> records = healthRecordService.getRecentHealthRecordsByElderly(elderlyId);
        return ResponseEntity.ok(records);
    }
}
